//
//  JYInitializer.m
//  s.
//
//  Created by 谈虎啸 on 2022/8/31.
//  Copyright © 2022 J. All rights reserved.
//

#import "JYInitializer.h"
#import <Masonry/Masonry.h>

@implementation JYInitializer
//MARK: - UIView
+ (UIView *)jy_initViewWithBgColor:(nullable UIColor *)bgColor
                             on:(nullable UIView *)superView
                         layout:(nullable void(^)(MASConstraintMaker *make))layout
{
    UIView *aView = [[UIView alloc] init];
    if (bgColor) {
        aView.backgroundColor = bgColor;
    }
    if (!superView) {
        return aView;
    }
    [superView addSubview:aView];
    if (layout) {
        [aView mas_makeConstraints:layout];
    }
    return aView;
}

//MARK: - UILabel
+ (UILabel *)jy_initLabelWithText:(nullable NSString *)text
                     textColor:(nullable UIColor *)textColor
                          font:(nullable UIFont *)font
                       bgColor:(nullable UIColor *)bgColor
                            on:(nullable UIView *)superView
                        layout:(nullable void(^)(MASConstraintMaker *make))layout
{
    UILabel *aLab = [[UILabel alloc] init];
    if (text) {
        aLab.text = text;
    }
    if (textColor) {
        aLab.textColor = textColor;
    }
    if (bgColor) {
        aLab.backgroundColor = bgColor;
    }
    if (font) {
        aLab.font = font;
    }
    if (!superView) {
        return aLab;
    }
    [superView addSubview:aLab];
    if (layout) {
        [aLab mas_makeConstraints:layout];
    }
    return aLab;
}
//MARK: - UIImageView
+ (UIImageView *)jy_initImageViewWithImage:(nullable UIImage *)image
                                     on:(nullable UIView *)superView
                                 layout:(nullable void(^)(MASConstraintMaker *make))layout
{
    UIImageView *aView = [[UIImageView alloc] init];
    if (image) {
        aView.image = image;
    }
    if (!superView) {
        return aView;
    }
    [superView addSubview:aView];
    if (layout) {
        [aView mas_makeConstraints:layout];
    }
    return aView;
}

//MARK: - UIButton
+ (UIButton *)jy_initButtonWithTarget:(nullable id)target
                            action:(nullable SEL)action
                             title:(nullable NSString *)title
                        titleColor:(nullable UIColor *)titleColor
                              font:(nullable UIFont *)font
                           bgColor:(nullable UIColor *)bgColor
                                on:(nullable UIView *)superView
                            layout:(nullable void(^)(MASConstraintMaker *make))layout
{
    UIButton *aBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    if (target && action) {
        [aBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    if (bgColor) {
        aBtn.backgroundColor = bgColor;
    }
    if (title) {
        [aBtn setTitle:title forState:UIControlStateNormal];
    }
    if (titleColor) {
        [aBtn setTitleColor:titleColor forState:UIControlStateNormal];
    }
    if (font) {
        aBtn.titleLabel.font = font;
    }
    if (!superView) {
        return aBtn;
    };
    [superView addSubview:aBtn];
    if (layout) {
        [aBtn mas_makeConstraints:layout];
    }
    return aBtn;
}
+ (UIButton *)jy_initButtonWithTarget:(nullable id)target
                            action:(nullable SEL)action
                             image:(nullable UIImage *)image
                                on:(nullable UIView *)superView
                            layout:(nullable void(^)(MASConstraintMaker *make))layout
{
    UIButton *aBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    if (target && action) {
        [aBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    if (image) {
        [aBtn setImage:image forState:(UIControlStateNormal)];
    }
    if (!superView) {
        return aBtn;
    };
    [superView addSubview:aBtn];
    if (layout) {
        [aBtn mas_makeConstraints:layout];
    }
    return aBtn;
}

/// 创建一个黄色渐变按钮
+ (JYGradientButton *)jy_initYellowButtonWithTarget:(nullable id)target
                                          action:(nullable SEL)action frame:(CGRect)frame title:(nullable NSString *)title on:(nullable UIView *)superView layout:(nullable void (^)(MASConstraintMaker * _Nonnull))layout {
    
    JYGradientButton * obj= [[JYGradientButton alloc] initWithFrame:frame];
    [obj setTitle:title forState:UIControlStateNormal];
    [obj addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [obj setTitleColor:KLColor(@"#FFFFFF") forState:UIControlStateNormal];
    obj.titleLabel.font =U_bfont(15);
    [superView addSubview:obj];
    if (layout) {
        [obj mas_makeConstraints:layout];
    }
    obj.colorsBlock(@[@"#C463C9",@"#ED609D"]);
    obj.pointBlock(CGPointMake(0, 0), CGPointMake(0, 1));
    [obj viewLayerBorderWidth:0 borderColor:[UIColor clearColor] cornerRadius:frame.size.height / 2];
    obj.layer.masksToBounds = NO;
    return obj;
}

+ (JYGradientButton *)jy_initGreenButtonWithTarget:(id)target action:(SEL)action frame:(CGRect)frame title:(NSString *)title on:(UIView *)superView layout:(void (^)(MASConstraintMaker * _Nonnull))layout {
    JYGradientButton * confimBtn= [[JYGradientButton alloc] initWithFrame:frame];
    [confimBtn setTitle:title forState:UIControlStateNormal];
    [confimBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [confimBtn setTitleColor:KLColor(@"#FFFFFF") forState:UIControlStateNormal];
    confimBtn.titleLabel.font =U_bfont(14);
    [superView addSubview:confimBtn];
    if (layout) {
        [confimBtn mas_makeConstraints:layout];
    }
    confimBtn.colorsBlock(@[@"#68CE90",@"#46A56B"]);
    confimBtn.pointBlock(CGPointMake(0, 0), CGPointMake(1, 0));
    [confimBtn viewLayerBorderWidth:0 borderColor:[UIColor clearColor] cornerRadius:frame.size.height / 2];
//    confimBtn.layer.masksToBounds = NO;
//    confimBtn.layer.shadowColor = KLColor(@"#C9CACA").CGColor;
//    confimBtn.layer.shadowOffset = CGSizeMake(0, 8);
//    confimBtn.layer.shadowOpacity = 0.3;
//    confimBtn.layer.shadowRadius = frame.size.height / 2;
    return confimBtn;
}

//MARK: - UITextField
+ (UITextField *)jy_initTextFieldWithText:(nullable NSString *)text
                             textColor:(nullable UIColor *)textColor
                                  font:(nullable UIFont *)font
                             placeHold:(nullable NSString *)placeHold
                        placeHoldColor:(nullable UIColor *)placeHoldColor
                         placeHoldFont:(nullable UIFont *)placeHoldFont
                               bgColor:(nullable UIColor *)bgColor
                              delegate:(nullable id<UITextFieldDelegate>)delegate
                                    on:(nullable UIView *)superView
                                layout:(nullable void(^)(MASConstraintMaker *make))layout
{
    UITextField *textField = [[UITextField alloc] init];
    if (text) {
        textField.text = text;
    }
    if (textColor) {
        textField.textColor = textColor;
    }
    if (font) {
        textField.font = font;
    }
    if (placeHold) {
        
        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
        if (placeHoldColor) {
            [attrs setValue:placeHoldColor forKey:NSForegroundColorAttributeName];
        }
        if (placeHoldFont) {
            [attrs setValue:placeHoldFont forKey:NSFontAttributeName];
        }
        
        NSAttributedString *holder = [[NSAttributedString alloc] initWithString:placeHold attributes:attrs];
        textField.attributedPlaceholder = holder;
    }
    if (bgColor) {
        textField.backgroundColor = bgColor;
    }
    if (delegate) {
        textField.delegate = delegate;
    }
    if (!superView) {
        return textField;
    }
    [superView addSubview:textField];
    
    if (layout) {
        [textField mas_makeConstraints:layout];
    }
    return textField;
}
//MARK: - ScrollView
+ (UIScrollView *)jy_initScrollViewWithBgColor:(nullable UIColor *)bgColor
                                   delegate:(nullable id<UIScrollViewDelegate>)delegate
                                         on:(nullable UIView *)superView
                                     layout:(nullable void(^)(MASConstraintMaker *make))layout
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 13.0, *)) {
        scrollView.automaticallyAdjustsScrollIndicatorInsets = NO;
    }
    if (bgColor) {
        scrollView.backgroundColor = bgColor;
    }
    if (delegate) {
        scrollView.delegate = delegate;
    }
    if (!superView) {
        return scrollView;
    }
    [superView addSubview:scrollView];
    
    if (layout) {
        [scrollView mas_makeConstraints:layout];
    }
    
    return scrollView;
}
//MARK: - UITableView
+ (UITableView *)jy_initTableViewWithBackgroundColor:(nullable UIColor *)bgColor
                                    style:(UITableViewStyle)style
                                 delegate:(nullable id<UITableViewDelegate, UITableViewDataSource>)delegate
                                       on:(nullable UIView *)superView
                                   layout:(nullable void(^)(MASConstraintMaker *make))layout{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(style)];
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    if (@available(iOS 13.0, *)) {
        tableView.automaticallyAdjustsScrollIndicatorInsets = NO;
    }
    if (@available(iOS 15.0, *)) {
        tableView.sectionHeaderTopPadding = 0;
    }
    if (bgColor) {
        tableView.backgroundColor = bgColor;
    }
    if (delegate) {
        tableView.delegate = delegate;
        tableView.dataSource = delegate;
    }
    if (!superView) {
        return tableView;
    }
    [superView addSubview:tableView];
    
    if (layout) {
        [tableView mas_makeConstraints:layout];
    }
    
    return tableView;
}

+ (UIButton *)jy_initSubmitStyleBtnWithTarget:(nullable id)target
                                    action:(nullable SEL)action
                                     title:(nullable NSString *)title
                                        on:(nullable UIView *)superView
                                    layout:(nullable void(^)(MASConstraintMaker *make))layout
{
    UIButton *submitBtn =
    [JYInitializer jy_initButtonWithTarget:target
                                        action:action title:title
                                    titleColor:JYColor_ff()
                                          font:U_font(16)
                                       bgColor:JYColor_Main()
                                            on:superView
                                        layout:layout];
    
    submitBtn.layer.cornerRadius = 24;
    return submitBtn;
    
}
+ (UIButton *)jy_initCancelStyleBtnWithTarget:(nullable id)target
                                    action:(nullable SEL)action
                                     title:(nullable NSString *)title
                                        on:(nullable UIView *)superView
                                    layout:(nullable void(^)(MASConstraintMaker *make))layout
{
    UIButton *cancelBtn =
    [JYInitializer jy_initButtonWithTarget:target
                                 action:action
                                  title:title
                             titleColor:JYColor_66()
                                   font:U_font(16)
                                bgColor:JYHexColor(0xf5f5f5)
                                     on:superView
                                 layout:layout];
    cancelBtn.layer.cornerRadius = 24;
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.borderColor = JYHexColor(0xCCCCCC).CGColor;
    
    return cancelBtn;
    
    
}
@end
